package com.suning.sawp.web.controller;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.commodity.MssCategory;
import com.suning.sawp.dto.commodity.MssCommoditySelfDto;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.commodity.MssCommoditySelfService;
import com.suning.sawp.intf.model.ModelErrorConstant;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.po.commodity.MssAggregationPage;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 码上省优选爆款接口 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("mss")
public class MssCommodityController extends DjBaseController {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MssCommodityController.class);

    /**
     * 码上省服务
     */
    @Autowired
    MssCommoditySelfService mssCommoditySelfService;

    /**
     * 店员服务
     */
    @Autowired
    StoreManService storeManService;

    /**
     * 查询优选,爆款商品 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param typeId
     * @param cateCode
     * @param currIndex
     * @param pageSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryCommodityPage.do")
    public MyGsonView queryCommodityPage(Principal principal, String typeId, String cateCode, Integer currIndex,
            Integer pageSize) {
        LOGGER.info("queryCommodityPage enter, principal={},typeId={},cateCode={},currIndex={},pageSize={}",
                new Object[] { principal.getName(), typeId, cateCode, currIndex, pageSize });
        // 工号
        String staffId = principal.getName();
        ReturnMsg<Map<String, Object>> returnMsg = new ReturnMsg<Map<String, Object>>();
        try {
            if (StringUtils.isBlank(typeId)) {
                returnMsg.setError(ModelErrorConstant.MODEL_ERR_E001, "入参typeId必填");
            } else {
                // 查询店员信息
                StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
                List<MssCommoditySelfDto> list = mssCommoditySelfService.getCommodityInfoList(
                        DJStringUtils.initCurrIndex(currIndex), DJStringUtils.initPageSize(pageSize), typeId, cateCode,
                        staffId, null == storeMan ? "" : storeMan.getStoreCode());
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("list", list);
                returnMsg.setData(data);
            }
        } catch (Exception e) {
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
            LOGGER.error("queryCommodityPage error,staffId=" + staffId + ",typeId=" + typeId, e);
        }
        return setGsonView(returnMsg);
    }

    /**
     * 查询聚合页数据 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryAggregationPage.do")
    public MyGsonView queryAggregationPage(Principal principal) {
        LOGGER.info("queryAggregationPage enter, staffId={}", principal.getName());
        ReturnMsg<Map<String, String>> returnMsg = new ReturnMsg<Map<String, String>>();
        try {
            // 查询聚合页信息
            MssAggregationPage page = mssCommoditySelfService.getMssAggregationPage(principal.getName());
            if (null != page) {
                // 记录不为空时返回结果
                Map<String, String> data = new HashMap<String, String>();
                data.put("linkUrl", page.getLinkUrl());
                data.put("picUrl", page.getPicUrl());
                returnMsg.setData(data);
            }
        } catch (Exception e) {
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
            LOGGER.error("queryAggregationPage error,staffId=" + principal.getName(), e);
        }
        return setGsonView(returnMsg);
    }

    /**
     * 查询码上省品类数据 功能描述: <br>
     * SCM配置 〈功能详细描述〉
     *
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryCateList.do")
    public MyGsonView queryCateList(Principal principal) {
        LOGGER.info("queryCateList enter, staffId={}", principal.getName());
        ReturnMsg<Map<String, Object>> returnMsg = new ReturnMsg<Map<String, Object>>();
        try {
            List<MssCategory> list = mssCommoditySelfService.queryCateList();
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", list);
            returnMsg.setData(data);
        } catch (Exception e) {
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
            LOGGER.error("queryCateList error,staffId=" + principal.getName(), e);
        }
        return setGsonView(returnMsg);
    }
    
}
